package cn.gz.demo.ui;

import android.app.AlertDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import androidx.databinding.DataBindingUtil;

import com.gyf.barlibrary.BarHide;

import cn.gz.demo.DialogBinding;
import cn.gz.demo.R;
import cn.gz.demo.ui.base.BaseActivity;

/**
 * Created by Liuyz on 2018/7/5.
 */

public class DialogActivity extends BaseActivity {
    private DialogBinding binding;
    AlertDialog navigationDialog;

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar
                .navigationBarColor(R.color.transparent)
//                .fullScreen(true)
                .statusBarDarkFont(false)
                .addTag("MainMine")  //给上面参数打标记，以后可以通过标记恢复
                .hideBar(BarHide.FLAG_SHOW_BAR).init();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_dialog);

        binding.btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                restBarView(1);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        showNavigationDialog();
                    }
                }, 500);
            }
        });
    }

    /**
     * 导航
     */
    public AlertDialog showNavigationDialog() {
        navigationDialog = new AlertDialog.Builder(context, R.style.customDialog).create();
        navigationDialog.setCancelable(false);
        navigationDialog.show();

        Window window = navigationDialog.getWindow();
        if (window != null) {
            window.getDecorView().setPadding(0, 0, 0, 0);
            window.getDecorView().setBackgroundColor(Color.WHITE);
            WindowManager.LayoutParams layoutParams = window.getAttributes();
            layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;
            layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
            window.setAttributes(layoutParams);
        }
        window.setGravity(Gravity.RIGHT);
        window.setWindowAnimations(R.style.slideRight);
        window.setBackgroundDrawableResource(R.mipmap.public_transparent_icon);
        window.setContentView(R.layout.public_work_operation_navigation_dialog);

        RelativeLayout navigation_icon = (RelativeLayout) window.findViewById(R.id.navigation_icon);
        navigation_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                restBarView(0);
                navigationDialog.dismiss();
            }
        });
        return navigationDialog;
    }

    public void restBarView(int type) {
        if (type == 0) {
            mImmersionBar
                    .reset()
                    .navigationBarColor(R.color.transparent)
//                .fullScreen(true)
                    .statusBarDarkFont(false)
                    .addTag("MainMine")  //给上面参数打标记，以后可以通过标记恢复
                    .hideBar(BarHide.FLAG_SHOW_BAR).init();
        } else {
            mImmersionBar
                    .reset()
                    .navigationBarColor(R.color.transparent)
//                .fullScreen(true)
                    .statusBarDarkFont(false)
                    .addTag("MainMine")  //给上面参数打标记，以后可以通过标记恢复
                    .hideBar(BarHide.FLAG_HIDE_STATUS_BAR).init();
        }
    }
}
